package com.grom.listener;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import com.grom.dao.DatabaseDao;
import com.grom.util.SqlFileUtil;
import com.holder.DBContextHolder;
import com.holder.DBUTil;

/**
 * Application Lifecycle Listener implementation class DataBaseInitListener
 * 
 */
public class DataBaseInitListener implements ServletContextListener {

	/**
	 * Default constructor.
	 */
	public DataBaseInitListener() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see ServletContextListener#contextInitialized(ServletContextEvent)
	 */
	public void contextInitialized(ServletContextEvent arg0) {
		DBContextHolder.setContextConnection(DatabaseDao.getConnection());
		try {
			List<String> sqls = SqlFileUtil.getSqlsFromFile("init_all.sql");
			for (String sql : sqls) {
				DBUTil.update(sql);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * @see ServletContextListener#contextDestroyed(ServletContextEvent)
	 */
	public void contextDestroyed(ServletContextEvent arg0) {
		// TODO Auto-generated method stub
	}

}
